package com.zlcdgroup.delegates

import com.zlcdg.study.kotlin.com.zlcdgroup.delegates.ValidatorUtil
import org.junit.jupiter.api.Assertions.assertDoesNotThrow
import org.junit.jupiter.api.Assertions.assertThrowsExactly
import org.junit.jupiter.api.Test
import java.text.SimpleDateFormat

/**
 *
 * @author pei
 * @date 2024/9/27 11:01
 */
class ValidatorUtilTest {

    @Test
    fun notBlank() {
        assertThrowsExactly(
            AppException::class.java, {
                ValidatorUtil.notBlank("", "空字符串异常")
            }, "验证空字符串"
        )
        assertDoesNotThrow {
            ValidatorUtil.notBlank("123", "空字符串异常")
        }
    }

    @Test
    fun sizeRange() {
        assertThrowsExactly(
            AppException::class.java, {
                ValidatorUtil.sizeRange("", 3, 20, "空字符串长度异常")
            }, "验证字符串长度"
        )
        assertDoesNotThrow {
            ValidatorUtil.sizeRange("pei", 3, 20, "空字符串长度异常")
        }
    }

    @Test
    fun numberRang() {
        assertThrowsExactly(
            AppException::class.java, {
                ValidatorUtil.numberRang(-5.34, 0, 10, "数值不在范围内异常")
            }, "验证数值范围"
        )
        assertDoesNotThrow {
            ValidatorUtil.numberRang(5.34, 0, 10, "数值不在范围内异常")
        }
    }

    @Test
    fun email() {
        assertThrowsExactly(
            AppException::class.java, {
                ValidatorUtil.email("", "邮件格式异常")
            }, "验证邮件格式"
        )
        assertDoesNotThrow {
            ValidatorUtil.email("peicl@saianfu.com", "邮件格式异常")
        }
    }

    @Test
    fun idCard() {
        assertThrowsExactly(
            AppException::class.java, {
                ValidatorUtil.idCard("2224061988082812345", "身份证号异常")
            }, "验证身份证号"
        )
        assertDoesNotThrow {
            ValidatorUtil.idCard("22240619880828123X", "身份证号异常")
        }
    }

    @Test
    fun mobilePhone() {
        assertThrowsExactly(
            AppException::class.java, {
                ValidatorUtil.mobilePhone("123", "电话号码异常")
            }, "验证电话号码"
        )
        assertDoesNotThrow {
            ValidatorUtil.mobilePhone("13648322209", "电话号码异常")
        }
    }

    @Test
    fun stringStart() {
        assertThrowsExactly(
            AppException::class.java, {
                ValidatorUtil.stringStart("", setOf("PJ10"), "字符串起始异常")
            }, "验证字符串起始"
        )
        assertDoesNotThrow {
            ValidatorUtil.stringStart("PJ1020240518", setOf("PJ10"), "字符串起始异常")
        }
    }

    @Test
    fun stringEnd() {
        assertThrowsExactly(
            AppException::class.java, {
                ValidatorUtil.stringEnd("", setOf("D"), "字符串结尾异常")
            }, "验证字符串结尾"
        )
        assertDoesNotThrow {
            ValidatorUtil.stringEnd("10234D", setOf("D"), "字符串结尾异常")
        }
    }

    @Test
    fun stringContains() {
        assertThrowsExactly(
            AppException::class.java, {
                ValidatorUtil.stringContains("", setOf("NB"), "字符串包含异常")
            }, "验证字符串包含"
        )
        assertDoesNotThrow {
            ValidatorUtil.stringContains("智能远传NB表", setOf("NB"), "字符串包含异常")
        }
    }

    @Test
    fun numberInSet() {
        assertThrowsExactly(
            AppException::class.java, {
                ValidatorUtil.numberInSet(0, setOf(1, 2, 3), "数值不在范围内")
            }, "验证数值范围"
        )
        assertDoesNotThrow {
            ValidatorUtil.numberInSet(2, setOf(1, 2, 3), "数值不在范围内")
        }
    }

    @Test
    fun stringInSet() {
        assertThrowsExactly(
            AppException::class.java, {
                ValidatorUtil.stringInSet("", setOf("1002", "1052", "1033"), "字符串不在范围内")
            }, "验证字符串值范围"
        )
        assertDoesNotThrow {
            ValidatorUtil.stringInSet("1052", setOf("1002", "1052", "1033"), "字符串不在范围内")
        }
    }

    @Test
    fun date() {
        assertThrowsExactly(
            AppException::class.java, {
                ValidatorUtil.date("20220828", dateFormat = SimpleDateFormat("yyyy-MM-dd"), "日期异常")
            }, "验证日期格式"
        )
        assertDoesNotThrow {
            ValidatorUtil.date("2022-08-28", dateFormat = SimpleDateFormat("yyyy-MM-dd"), "日期异常")
        }
    }

    @Test
    fun xss() {
        assertThrowsExactly(
            AppException::class.java, {
                ValidatorUtil.xss("<javascript>test</javascript>", "xss异常")
            }, "验证xss"
        )
        assertDoesNotThrow {
            ValidatorUtil.xss("javascript", "xss异常")
        }
    }
}